<?php

require_once './application/core/CoreBase.php';
require_once './application/core/Log/logCore.php';
require_once './application/models/Tables/logFilters.php';
require_once './application/models/Tables/cache.php';
require_once './application/entity/log/datafilter.php';

class logData extends IndexCore
{

    private $_data;
    private $_tmp;
    private $_name;
    private $_table;
    private $_obj;

    //cree les colones
    private function createColumTab($array)
    {
        $arrayColums = array();
        for ($i = 0; $i < sizeof($array); $i++)
            $arrayColums[] = $array[$i];
        return $arrayColums;
    }

    //remplie les colones
    private function createDataTab($array, $string)
    {
        $data = preg_split("/[\ ]{4}/", $string);
        $arrayData = array();
        foreach ($data as $val)
        {
            $n = sizeof($array);
            for ($i = 0, $n = sizeof($array); $i < $n; $i++)
            {
                if (@preg_match('/' . $array[$i] . '/', $val, $rslt))
                    $this->_tmp .= $rslt[0] . ";;;";
                else
                    $this->_tmp .= "&#216;;;";
            }
            $this->_tmp = preg_replace("/;;;$/", "", $this->_tmp);
            $arrayData[] = $this->_tmp;
            $this->_tmp = null;
        }
        return $arrayData;
    }

    public function parseFilterData($xml, $name)
    {
        if ($name != '-')
        {
            $this->_data = $this->getFilterBase($name);
            $col = array();
            $regexp = array();
            for ($i = 0; $i < ( sizeof($this->_data) - 1 ); $i = $i + 2)
            {
                $col[] = $this->_data[$i];
                $regexp[] = $this->_data[$i + 1];
            }
        } else
        {
            $col[0] = 'Log';
            $regexp[0] = '.*';
        }
        $this->_data[0] = $this->createColumTab($col);
        $this->_data[1] = $this->createDataTab($regexp, $xml);
        return $this->_data;
    }

    //Parse la la ligne de la base
    private function getFilterBase($name)
    {
        $this->_table = new logFilters();
        $this->_data = $this->_table->getLogFilter($name);
        $this->_data = explode(";", $this->_data[0]['Filter']);
        return $this->_data;
    }

    public function getInfoFilterBase()
    {
        $this->_name = $this->_controller->getRequest()->getParam('name');
        $this->_data = $this->getFilterBase($this->_name);
        $this->_obj = new datafilter();
        $this->_obj->name = $this->_name;
        $this->_obj->list = $this->_data;
        echo Zend_Json_Encoder::encode($this->_obj);
    }

}